package com.jbh.wanandroidkotlin.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

open class BaseAdapter<T>(private val context:Context,private val itemView:Int, private val list:List<T>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val itemView = LayoutInflater.from(context).inflate(itemView,parent,false)
        return MyViewHolder(itemView)
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        list[position]?.let {
            onBindView(holder,it)
            holder.itemView.setOnClickListener {
                onItemClickListener?.OnItemClick(list[position])
            }
        }
    }


    override fun getItemCount(): Int {
        return list?.let {
            it.size
        }
    }


    open fun onBindView(holder: RecyclerView.ViewHolder, t: T){

    }

    inner class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView)


    interface OnItemClickListener<T>{
        fun OnItemClick( t : T ?= null)
    }


    var onItemClickListener : OnItemClickListener<T> ?= null

    @JvmName("setOnItemClickListener1")
    fun setOnItemClickListener(onItemClick: OnItemClickListener<T> ?){
        onItemClickListener=onItemClick
    }
}